/* eslint-disable camelcase */
import request from '@/utils/request'
import { RootThunkAction } from '@/types/store'
import { ApiResponse, Article } from '@/types/data'

// 获取用户频道信息
// 1.登录用户:正常发请求,
// 2.没有登录:从本地取,本地没有数据在发请求
export const getArticles = (
  channelId: string,
  timestamp: number
): RootThunkAction => {
  return async (dispatch) => {
    const res = await request.get<
      ApiResponse<{
        pre_timestamp: string
        results: Article[]
      }>
    >('/articles', {
      params: {
        channel_id: channelId,
        timestamp
      }
    })
    console.log(res, 'article')
    dispatch({
      type: 'article/getArticles',
      payload: {
        channelId,
        timestamp: res.data.data.pre_timestamp,
        articles: res.data.data.results
      }
    })
  }
}

export const getNewArticles = (
  channelId: string,
  timestamp: number
): RootThunkAction => {
  return async (dispatch) => {
    const res = await request.get<
      ApiResponse<{
        pre_timestamp: string
        results: Article[]
      }>
    >('/articles', {
      params: {
        channel_id: channelId,
        timestamp
      }
    })
    console.log(res, 'article')
    dispatch({
      type: 'article/getNewArticles',
      payload: {
        channelId,
        timestamp: res.data.data.pre_timestamp,
        articles: res.data.data.results
      }
    })
  }
}
